package com.kingdee.eas.hr.quali.handler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.org.StateEnum;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.quali.QualificationsClassCollection;
import com.kingdee.eas.hr.quali.QualificationsClassFactory;
import com.kingdee.eas.hr.quali.QualificationsStandardInfo;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

public class QualificationsStandardEditHandler extends EditHandler {
	@Override
	protected void beforeSave(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)throws SHRWebException {
		super.beforeSave(request, response, model);
		QualificationsStandardInfo info = (QualificationsStandardInfo) model;
		if(info.getState() == null){
			info.setState(StateEnum.DISABLE);
		}
	}
	
	
	@Override
	protected void afterCreateNewModel(HttpServletRequest request, HttpServletResponse response,CoreBaseInfo coreBaseInfo) throws SHRWebException {
		super.afterCreateNewModel(request, response, coreBaseInfo);
		QualificationsStandardInfo info = (QualificationsStandardInfo) coreBaseInfo;
		if(info.getState() == null){
			info.setState(StateEnum.DISABLE);
		}
	}


	public String getDataInfoAction(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) throws SHRWebException {
		String classId = request.getParameter("classId");
		Context ctx = SHRContext.getInstance().getContext();
		Map<String,Object> data = new HashMap<String,Object>();
		Map<String,Object> dataMap = new HashMap<String,Object>();
		try {
			QualificationsClassCollection colls = QualificationsClassFactory.getLocalInstance(ctx)
					.getQualificationsClassCollection("select quaFamily.id,quaFamily.name,name where id = '"+classId+"'");
			if(colls.size() > 0){
				data.put("status", 200);
				dataMap.put("familyId", colls.get(0).getQuaFamily().getId());
				dataMap.put("familyName", colls.get(0).getQuaFamily().getName());
				dataMap.put("names", dataMap.get("familyName")+"/"+colls.get(0).getName());
				data.put("dataMap", dataMap);
			}else{
				data.put("status", 500);
			}
		} catch (BOSException e) {
			e.printStackTrace();
		}
		JSONUtils.writeJson(response, data);
		return null;
	}
}
